import 'dart:ui';

import 'package:get/get.dart';
import 'package:FiduMart/core/utils/logger.dart';
import 'package:FiduMart/core/utils/pref_utils.dart';

import '../../constants/constants.dart';

class Setting extends GetxController {
  Rx<String> lang = Rx('');

  static Future<Locale?> getLocale() async {
    Locale? locale;
    final pref = Get.find<PrefUtils>();
    String? lang = await pref.getLang();
    Logger.log('lang:$lang');
    // Logger.log("Get.deviceLocale:${Get.deviceLocale}");
    if (lang == null || lang == '') {
      locale = Get.deviceLocale;
      if (!langmap.containsKey(locale)) {
        locale = Locale('en', 'US');
      }
      await pref.setLang(locale.toString());
    } else {
      // Logger.log("langlist:$lang");
      var langlist = lang.split('_');
      if (langlist.length == 1) {
        if (langlist[0] == 'zh') {
          locale = Locale('zh', 'HK');
        } else {
          locale = Locale('en', 'US');
        }
      } else if (langlist.length == 2) {
        locale = Locale(langlist[0], langlist[1]);
      }
    }
    return locale;
  }

  static updateLang(String lang) async {
    Logger.log("shezhi:${lang}");
    final pref = Get.find<PrefUtils>();
    bool b = await pref.setLang(lang);
    if (b == false) {
    } else {
      var langs = lang.split('_');
      Get.updateLocale(Locale(langs[0], langs[1]));
    }
  }
}
